## Perspective Taking and Security Dilemma Thinking: replication data
#Kertzer, Brutger and Quek
#October 1, 2023

####### SCS Replication 2.R: this R file contains the code necessary to run the attribution asymmetry analysis in the main text - run SCS Replication 1.R first!
# All of the following analyses were carried out using R version 4.3.1 GUI 1.77 (Big Sur ARM build 8238) on an M1 Pro Macbook Pro running MacOS Ventura. 

####### Sample size discussion in main text
nrow(usa1) #N=1820
nrow(china1) #N=1485
nrow(usa2) #N=627

####### Attribution module: identifying security dilemma thinking

#### Figure 2: Respondents display the attribution asymmetry consistent with security dilemma thinking

set.seed(43215)
B <- 1500

out.us1 <- matrix(NA, nrow=B, ncol=4)
for (i in 1:B){
	j <- sample(1:nrow(usa1), nrow(usa1), replace=TRUE)
	temp <- usa1[j,]
	out.us1[i,1] <- mean(temp$attrChInc, na.rm=TRUE)	
	out.us1[i,2] <- mean(temp$attrUsInc, na.rm=TRUE)	
	k <- sample(1:nrow(china1), nrow(china1), replace=TRUE)
	temp <- china1[k,]
	out.us1[i,3] <- mean(temp$attrUsInc, na.rm=TRUE)	
	out.us1[i,4] <- mean(temp$attrChInc, na.rm=TRUE)	
}

out.us1a <- as.data.frame(rbind(cbind(apply(out.us1, 2, mean, na.rm=TRUE), apply(out.us1, 2, quantile, 0.025, na.rm=TRUE), apply(out.us1, 2, quantile, 0.975,na.rm=TRUE))))
colnames(out.us1a) <- c("X", "Low", "High")
out.us1a$DV <- rep(c("They increase", "We increase"),2)
out.us1a$Sample <- rep(c("USA", "China"), each=2)


# Hawaii data (weighted)
usa2w <- usa2[which(!is.na(usa2$eWeight)),]
out.us2 <- matrix(NA, nrow=B, ncol=2)
for (i in 1:B){
	j <- sample(1:nrow(usa2w), nrow(usa2w), replace=TRUE)
	temp <- usa2w[j,]
	out.us2[i,1] <- weighted.mean(x=temp$attrChInc, w=temp$eWeight, na.rm=TRUE)	
	out.us2[i,2] <- weighted.mean(x=temp$attrUsInc, w=temp$eWeight, na.rm=TRUE)	
}

out.us2a <- as.data.frame(rbind(cbind(apply(out.us2, 2, mean, na.rm=TRUE), apply(out.us2, 2, quantile, 0.025, na.rm=TRUE), apply(out.us2, 2, quantile, 0.975,na.rm=TRUE))))
colnames(out.us2a) <- c("X", "Low", "High")
out.us2a$DV <- c("They increase", "We increase")
out.us2a$Sample <- rep(c("USA"), each=2)

#Plot comparing SCS results with Hawaii results
fig2 <- rbind(out.us1a,out.us2a)
fig2$Context <- "South China Sea (Study 1)"
fig2$Context[5:6] <- "Hawaii (Study 2)"
fig2$SampleContext <- paste(fig2$Sample, fig2$Context)
fig2$Context <- as.factor(fig2$Context)
fig2$Context <- relevel(fig2$Context, ref="South China Sea (Study 1)")

fig2.plot <- ggplot(data=fig2, aes(x=DV,y=X, color=Sample, shape=Sample, lty=Context)) + geom_point() + geom_segment(aes(x=DV, y=Low, xend=DV, yend=High)) + theme_bw() + geom_line(aes(x=DV, y=X, group=SampleContext, alpha=0.8)) + labs(y="Attribution (Defensive - Offensive)", x="") + guides(alpha="none", color=guide_legend(order=1), shape=guide_legend(order=1), line=guide_legend(order=2)) + ylim(1,7) + scale_colour_brewer(palette="Set1")
ggsave(file="fig2.pdf", plot=fig2.plot, height=4.5, width=6.3, units="in")

#Quantities of interest discussed in text

#Size of attribution asymmetry in each country
CH1.asym <- mean(china1$attrBias, na.rm=TRUE)
round(CH1.asym, digits=2) #Attribution asymmetry in China: 2.89 points
t.test(c(china1$attrUsInc, china1$attrChInc) ~ rep(c(1,0),each=nrow(china1))) #p < 0.001

US1.asym <- mean(usa1$attrBias, na.rm=TRUE) 
round(US1.asym, digits=2) #Attribution asymmetry in US: 0.94 points
t.test(c(usa1$attrChInc, usa1$attrUsInc) ~ rep(c(1,0),each=nrow(usa1))) #p < 0.001
round(CH1.asym/US1.asym, digits=1) # Attribution asymmetry 3.1 times greater in China than in the US

#What's the attribution asymmetry in Hawaii experiment?
usa2$attrBias <- usa2$attrChInc - usa2$attrUsInc
round(mean(usa2$attrBias, na.rm=TRUE), digits=2) #1.29 points
round(CH1.asym/mean(usa2$attrBias, na.rm=TRUE), digits=1) #2.2 times larger in China than in US sample in hawaii experiment

#How much does geographic proximity account for?
# SCS attribution asymmetry
SCS.asym <- CH1.asym - US1.asym #1.944
# SCS (Chinese) vs Hawaii (American attribution asymmetry)
Hawaii.asym <- mean(china1$attrBias, na.rm=TRUE) - mean(usa2$attrBias, na.rm=TRUE) #1.591
diff.asym <- SCS.asym - Hawaii.asym # .353
# proportion of asymmetry decline shifting to Hawaii
round(diff.asym/SCS.asym, digits=2) #0.18

#Do Americans perceive Chinese escalation as more offensively motivated in Hawaii than in the South China Sea?
combined.df2 <- data.frame(asymmetry=c(usa1$attrChInc, usa2$attrChInc), hawaii=c(rep(0,nrow(usa1)), rep(1,nrow(usa2))))
t.test(asymmetry ~ hawaii, data=combined.df2) #p < 0.001

##### Figure 3: Chinese and Americans perceive the US-China relationship very differently

dat <- data.frame(Intentions=c(usa1$imgNegInt1,china1$imgNegInt1), Capabilities=c(usa1$imgParity1,china1$imgParity1), Sample=c(rep("USA",nrow(usa1)), rep("China",nrow(china1))))

#Violin plots
fig3a <- ggplot(dat, aes(y=Intentions, x=Sample)) + geom_violin(aes(fill=Sample), alpha=0.8) + theme_bw() + labs(y="Image of Other: Negative Intentions", x="Sample") + scale_fill_hue(h=c(10,250)) + theme(legend.position="none") + geom_boxplot(notch=FALSE, na.rm=TRUE, width=0.15)
ggsave(file="fig3a.pdf", plot=fig3a, height=4, width=4, units="in")

fig3b <- ggplot(dat, aes(y=Capabilities, x=Sample)) + geom_violin(aes(fill=Sample), alpha=0.8) + theme_bw() + labs(y="Image of Other: Equal Capabilities", x="Sample") + scale_fill_hue(h=c(10,250)) + theme(legend.position="none") + geom_boxplot(notch=FALSE, na.rm=TRUE, width=0.15)
ggsave(file="fig3b.pdf", plot=fig3b, height=4, width=4, units="in")

#Quantities of interest discussed in text

#Comparison of Chinese and American perceived negative intentions
wilcox.test(Intentions ~ Sample, data=dat) #W=1385166 p < 0.001
#Comparison of Chinese and American perceived relative capabilities
wilcox.test(Capabilities ~ Sample, data=dat) #W=611282 p < 0.001

#Intraclass correlation estimates

icc.df <- data.frame(attrChInc=c(usa1$attrChInc, china1$attrChInc), attrUsInc=c(usa1$attrUsInc, china1$attrUsInc), country=c(rep("USA", nrow(usa1)), rep("China", nrow(china1))))
icc.df$country <- as.factor(icc.df$country)

mod.0 <- lmer(attrChInc ~ (1|country), data=icc.df)
icc(mod.0) #0.383
1/0.383 #2.61 times more variation within each sample than between

mod.0 <- lmer(attrUsInc ~ (1|country), data=icc.df)
icc(mod.0) #0.437
1/0.437 #2.29 times more variation within each sample than between

#### Table 1: Explaining crossnational variation in attribution asymmetry

mod.1 <- lm(attrBias ~ imgNegInt1 + imgParity1, data=usa1)
mod.1b <- lm(attrBias ~ imgNegInt1 + imgParity1 + natAboutYou + milAssert1 + educ + male + ideo1, data=usa1) 
mod.2 <- lm(attrBias ~ imgNegInt1 + imgParity1, data=china1)
mod.2b <- lm(attrBias ~ imgNegInt1 + imgParity1 + natAboutYou + milAssert1 + educ + male + partyMember, data=china1)

stargazer(mod.1,mod.1b,mod.2,mod.2b, title="Explaining crossnational variation in attribution asymmetry", omit.stat=c("LL", "ser", "f"), style="apsr", digits=3, label="tab:attr", covariate.labels=c("Negative intentions", "Equal capabilities", "Nationalism", "Military assertiveness", "Education", "Male", "Ideology", "Party Member", "Constant"), dep.var.labels.include=FALSE, column.labels=c("US Sample","China Sample"), column.separate=c(2,2))